package BandB.Tool.GPhoneManager.FileExplorer.Installer;

import java.util.ArrayList;
import java.util.List;

import BandB.Tool.GPhoneManager.R;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;


public class InstallerAdapter extends BaseAdapter {

	private static final String TAG = InstallerAdapter.class.getSimpleName();
	private Context mContext;
	private List<ApplicationInfo> mInfoList;
	private int mSize;
	private LayoutInflater mInflater;
	private PackageManager pm;

	// private boolean[] mSelectedList;

	public InstallerAdapter(Context c, List<ApplicationInfo> infoList) {
		mContext = c;
		mInfoList = infoList;
		mSize = mInfoList.size();
		mInflater = LayoutInflater.from(mContext);
		pm = c.getPackageManager();
		// mSelectedList = new boolean[mSize];

	}

	public void setCheckedAt(int position) {
		ApplicationInfo info = (ApplicationInfo) getItem(position);
		info.setChecked(!info.isChecked());
		notifyDataSetChanged();
	}

	public void selectAll() {
		for (int i = 0; i < mSize; i++) {
			ApplicationInfo info = (ApplicationInfo) getItem(i);
			info.setChecked(true);
		}
		notifyDataSetChanged();
	}

	public void selectNone() {
		for (int i = 0; i < mSize; i++) {
			ApplicationInfo info = (ApplicationInfo) getItem(i);
			info.setChecked(false);
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mInfoList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mInfoList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.backup_list_item, null);
		}

		ImageView icon = (ImageView) convertView
				.findViewById(R.id.imgViewBackUpList);
		TextView appName = (TextView) convertView.findViewById(R.id.app_name);
		TextView app_date = (TextView) convertView
				.findViewById(R.id.backup_date);
		TextView appVersion = (TextView) convertView
				.findViewById(R.id.app_version);
		TextView appSize = (TextView) convertView.findViewById(R.id.apk_size);
		CheckBox checked = (CheckBox) convertView
				.findViewById(R.id.recover_checkbox);

		ApplicationInfo info = (ApplicationInfo) getItem(position);
		android.content.pm.ApplicationInfo pInfo = info.getInfo();

		String packageName = pInfo.packageName;
		Drawable drawable = pInfo.loadIcon(pm);
		try {
			appVersion.setText(pm.getPackageInfo(packageName, 0).versionName);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e(TAG, "package not found:" + packageName);
		}

		icon.setImageDrawable(drawable);
		appName.setText(pInfo.loadLabel(pm));
		app_date.setText(info.getLastModified());
		appSize.setText(info.getSize());
		checked.setChecked(info.isChecked());

		return convertView;

	}

	public List<ApplicationInfo> getCheckedItem() {
		// TODO Auto-generated method stub
		List<ApplicationInfo> checkList = new ArrayList<ApplicationInfo>();
		int count = getCount();
		for (int i = 0; i < count; i++) {
			ApplicationInfo info = (ApplicationInfo) getItem(i);
			if (info.isChecked()) {
				checkList.add(info);
			}
		}
		return checkList;
	}
}